import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import { Layout } from '@fl/utils/routerHelper'
import { createRouter, createWebHistory } from 'vue-router'
// import { adminRoute, businessManagementRoute } from './routes'
// import { wujieRouteDict } from './wujie'
const WujieLayout = () => import('@/layout/wujie-layout.vue')
const WujieComponent = () => import('@/pages/wujie/joint-good-house.vue')

// 联合管理路由
export const constantRouterMap: AppRouteRecordRaw[] = [
    {
        path: '',
        component: Layout,
        redirect: '/joint',
        // redirect: String(window.sessionStorage.getItem('FIRST_REDIRECT')),
        meta: {
            hidden: true,
            noTagsView: true,
        },
    },
    {
        children: [
            {
                component: () => import('@fl/views/redirect/redirect.vue'),
                meta: {},
                name: 'redirect',
                path: '/redirect/:path(.*)',
            },

            {
                component: () => import('@fl/views/error/404.vue'),
                meta: {
                    hidden: true,
                    noTagsView: true,
                    title: '404',
                },
                name: 'NoFind',
                path: '/404',
            },
        ],
        component: Layout,
        meta: {
            hidden: true,
            noTagsView: true,
        },
        name: 'Redirect',
        path: '/redirect',
    },
    {
        children: [
            {
                component: () => import('@/pages/Personal/PersonalCenter.vue'),
                meta: {
                    hidden: true,
                    title: '个人中心',
                },
                name: 'PersonalCenter',
                path: 'personal-center',
            },
        ],
        component: Layout,
        meta: {
            hidden: true,
            title: '个人中心',
        },
        name: 'Personal',
        path: '/personal',
        redirect: '/personal/personal-center',
    },
    {
        component: () => import('@/pages/login/index.vue'),
        meta: {
            hidden: true,
            noTagsView: true,
            title: '登录',
        },
        name: 'login',
        path: '/login',
    },

    {
        children: [
            {
                component: WujieComponent,
                meta: {
                    title: '武汉好房',
                },
                name: 'good-house-index',
                path: '',
            },
        ],
        component: WujieLayout,
        meta: {
            hidden: true,
            title: '武汉好房',
        },
        name: 'good-house',
        path: '/good-house',
    },

    // {
    //     component: () => import('@/pages/register/register.vue'),
    //     meta: {
    //         hidden: true,
    //         noTagsView: true,
    //         title: '注册',
    //     },
    //     name: 'register',
    //     path: '/register',
    // },
    // {
    //     component: () => import('@/pages/forget-password/forget-password.vue'),
    //     meta: {
    //         hidden: true,
    //         noTagsView: true,
    //         title: '忘记密码',
    //     },
    //     name: 'forget-password',
    //     path: '/forget-password',
    // },
]

const addRoute = {
    name: 'joint',
    path: '/joint',
    redirect: '/joint/add-enterprise',
    component: Layout,
    meta: {
        title: '联合运营',
        icon: '#',
        noCache: false,
        link: null,
    },
    children: [
        {
            name: 'add-enterprise',
            path: '/joint/add-enterprise',
            component: () => import('@/pages/company-org/component/add.vue'),
            meta: {
                name: 'add',
                title: '新增企业',
                // affix: true,
            },
        },
    ],
}

// 所有路由：asyncRouterMap
export const asyncRouterMap: AppRouteRecordRaw[] = [
    // businessManagementRoute,
    // adminRoute,
    // ...Object.values(wujieRouteDict).flat(),
    addRoute,
]

const router = createRouter({
    history: createWebHistory(),
    routes: constantRouterMap as RouteRecordRaw[],
    scrollBehavior: () => ({ left: 0, top: 0 }),
    strict: true,
})

export function resetRouter(): void {
    const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
    router.getRoutes().forEach((route) => {
        const { name } = route
        if (name && !resetWhiteNameList.includes(name as string))
            router.hasRoute(name) && router.removeRoute(name)
    })
}

export function setupRouter(app: App<Element>) {
    app.use(router)
}

export default router
